<template>
  <div>
    <div v-for="(item, index) in list" :key="index" class="item">
      <div class="card-box" @click="getDetail(item.id)">
        {{ item.title }}
      </div>
    </div>
  </div>
</template>

<script>
import request from "../../../utils/request";

export default {
  data() {
    return {
      list: [],
      limit: 10,
      total: 0,
      listenUrl: "listening",
      readUrl: "reading",
      writeUrl: "writing",
      type: "",
    };
  },

  onLoad: function (option) {
    let params = {};
    this.type = option.type;

    if (this.type === "listen") {
      params.url = this.listenUrl;
    } else if (this.type === "full") {
      params.url = this.readUrl;
      params.type = 1;
    } else if (this.type === "match") {
      params.url = this.readUrl;
      params.type = 2;
    } else if (this.type === "read") {
      params.url = this.readUrl;
      params.type = 3;
    } else if (this.type === "write") {
      params.url = this.writeUrl;
      params.type = 1;
    } else if (this.type === "translation") {
      params.url = this.writeUrl;
      params.type = 2;
    }

    this.getList(params);
  },

  methods: {
    async getList(params) {
      let response = await request({
        url: `/exam_service/` + params.url + `/query/1/${this.limit}`,
        method: "POST",
        data: params,
      });

      this.list = response.data.rows;
      this.total = response.data.total;
    },

    async getDetail(id) {
      if (this.type === "listen") {
        wx.navigateTo({
          url: `/pages/index/listenDetail/main?id=${id}`,
        });
      } else if (this.type === "full") {
        wx.navigateTo({
          url: `/pages/index/readDetail/main?id=${id}&type=1`,
        });
      } else if (this.type === "match") {
        wx.navigateTo({
          url: `/pages/index/readDetail/main?id=${id}&type=2`,
        });
      } else if (this.type === "read") {
        wx.navigateTo({
          url: `/pages/index/readDetail/main?id=${id}&type=3`,
        });
      } else if (this.type === "write") {
        wx.navigateTo({
          url: `/pages/index/writeDetail/main?id=${id}&type=1`,
        });
      } else if (this.type === "translation") {
        wx.navigateTo({
          url: `/pages/index/writeDetail/main?id=${id}&type=2`,
        });
      }
    },
  },
  onReachBottom() {
    if (this.limit >= this.total) {
      //没有下一页了
      wx.showToast({
        title: "已到达底部",
      });
    } else {
      this.limit += 5;
      this.getList();
    }
  },
};
</script>

<style>
.card-box {
  border-radius: 30rpx;
  box-shadow: 18rpx 18rpx 30rpx #d1d9e6, -18rpx -18rpx 30rpx #fff;
  margin-top: 20rpx;
  margin-bottom: 20rpx;
  padding: 30rpx;
  text-align: left;
  font-size: 40rpx;
}
</style>
